package DB;

import Model.Team;

import java.sql.*;
import java.util.ArrayList;

public class TeamDB implements TeamDBIF {

    private Connection conn;

    public TeamDB() {
        conn = ConnectionDB.getInstance().getConn();
    }

    @Override
    public int insertTeam(Team team) {
        int rc = -1;
        PreparedStatement pstmt = null;

        String insert = "INSERT INTO Team (teamId, name, startDate, endDate, semesterId)" + "VALUES (?,?,?,?,?)";

        System.out.println(insert);

        try {
            pstmt = conn.prepareStatement(insert);
            pstmt.setInt(1, team.getId());
            pstmt.setString(2, team.getName());
            pstmt.setString(3, team.getStartDate());
            pstmt.setString(4, team.getEndDate());
            pstmt.setInt(5, team.getSemester().getId());
            rc = pstmt.executeUpdate();
        }
        catch (SQLException sqlE) {
            System.out.println("SQL Error: " + sqlE.getMessage());
        }
        catch (Exception e) {
            e.getMessage();
        }

        return rc;
    }

    @Override
    public Team findTeam(int id) {
        return singleWhere("teamId = " + id, false);
    }

    @Override
    public Team findTeamByName(String name) {
        return singleWhere("name = '" + name + "'", false);
    }

    @Override
    public ArrayList<Team> getAllTeams(boolean retrieveAssociation) {
        return miscWhere("", false);
    }

    @Override
    public int updateTeam(Team team) {
        Team teamObj = team;
        int rc = -1;

        String query="UPDATE Team SET " +
                "name ='" +teamObj.getName() + "', " +
                "startDate ='" + teamObj.getStartDate() + "', " +
                "endDate ='" + teamObj.getEndDate() + "' " +

                " WHERE teamId = " + teamObj.getId() + "";

        System.out.println("Update query: " + query);

        try {
            Statement stmt = conn.createStatement();
            stmt.setQueryTimeout(5);
            rc = stmt.executeUpdate(query);
            stmt.close();
        }
        catch(Exception e){
            System.out.println("Update exception in Team db: " +e);
        }
        return(rc);
    }

    @Override
    public int deleteTeam(int id) {
        int rc = -1;

        String query = "DELETE FROM Team WHERE teamId = '" + id + "'";

        System.out.println(query);

        try {
            Statement stmt = conn.createStatement();
            stmt.setQueryTimeout(5);
            rc = stmt.executeUpdate(query);
            stmt.close();
        }
        catch(Exception e){
            System.out.println("Delete exception in team db: " + e);
        }
        return(rc);    
    }

    //Finding a single Team
    private Team singleWhere(String wClause, boolean retrieveAssociation) {
        ResultSet results;
        Team teamObj = new Team();

        String query = buildQuery(wClause);
        System.out.println(query);

        try {
            Statement stmt = conn.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            if (results.next()) {
                teamObj = buildTeam(results);
                stmt.close();
            }
            else {
                teamObj = null;
            }
        }
        catch (Exception e) {
            System.out.println("Query exception: " + e);
        }

        return teamObj;
    }

    //Finding all Teams
    private ArrayList<Team> miscWhere(String wClause, boolean retrieveAssociation) {
        ResultSet results;
        ArrayList<Team> list = new ArrayList<Team>();

        String query = buildQuery(wClause);

        try {
            Statement stmt = conn.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            while (results.next()) {
                Team teamObj = new Team();
                teamObj = buildTeam(results);
                list.add(teamObj);
            }

            stmt.close();
        }
        catch (Exception e) {
            System.out.println("Query exception: " + e);
            e.printStackTrace();
        }

        return list;
    }

    //Method to build the query
    private String buildQuery(String wClause) {
        String query = "SELECT teamId, name, startDate, endDate, semesterId FROM Team ";

        if (wClause.length()>0) {
            query = query + " WHERE " + wClause;
        }

        return query;
    }

    //Method to build a Team object
    private Team buildTeam(ResultSet results) {

        Team teamObj = new Team();
        SemesterDB semesterDB = new SemesterDB();

        try {
            teamObj.setId(results.getInt("teamId"));
            teamObj.setName(results.getString("name"));
            teamObj.setStartDate(results.getString("startDate"));
            teamObj.setEndDate(results.getString("endDate"));
            teamObj.setSemester(semesterDB.findSemester(results.getInt("semesterId")));
        }
        catch (Exception e) {
            System.out.println("Error building Team object");
        }

        return teamObj;
    }

    public int teamGetMax() {
        int nextId = GetMax.getMaxId("SELECT max(teamId) from Team");

        nextId = nextId +1;

        return nextId;
    }


}
